import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { useTaskStore } from './taskStore'
import type { SystemList, CustomList, ListType } from '@/types/list'

// 兼容性接口
export interface List {
  id: string
  name: string
  icon?: string
  color?: string
  type: 'system' | 'custom'
}

export const useListStore = defineStore('listStore', () => {
  // 系统清单数据
  const systemListsData = ref<SystemList[]>([
    {
      id: 'today',
      name: '今天',
      icon: 'mdi-calendar-today',
      color: 'blue',
      type: 'system' as const,
      filter: 'today'
    },
    {
      id: 'tomorrow',
      name: '明天',
      icon: 'mdi-calendar-arrow-right',
      color: 'orange',
      type: 'system' as const,
      filter: 'tomorrow'
    },
    {
      id: 'week',
      name: '本周',
      icon: 'mdi-calendar-week',
      color: 'green',
      type: 'system' as const,
      filter: 'week'
    },
    {
      id: 'important',
      name: '重要',
      icon: 'mdi-star',
      color: 'red',
      type: 'system' as const,
      filter: 'important'
    },
    {
      id: 'planned',
      name: '已计划',
      icon: 'mdi-calendar-clock',
      color: 'purple',
      type: 'system' as const,
      filter: 'planned'
    },
    {
      id: 'tasks',
      name: '任务',
      icon: 'mdi-format-list-bulleted',
      color: 'blue',
      type: 'system' as const,
      filter: 'all'
    },
    {
      id: 'completed',
      name: '已完成',
      icon: 'mdi-check-circle',
      color: 'grey',
      type: 'system' as const,
      filter: 'completed'
    }
  ])

  // 自定义清单数据
  const customListsData = ref<CustomList[]>([])

  // 计算属性
  const systemLists = computed(() => systemListsData.value)
  const customLists = computed(() => customListsData.value)
  
  // 兼容性：合并所有清单
  const lists = computed(() => [
    ...systemListsData.value,
    ...customListsData.value
  ])

  const getListById = (id: string) => {
    return lists.value.find(list => list.id === id)
  }

  // 获取清单任务计数
  const getListTaskCount = (listId: string) => {
    const taskStore = useTaskStore()
    switch (listId) {
      case 'today':
        return taskStore.todayCount
      case 'tomorrow':
        return taskStore.tomorrowCount
      case 'week':
        return taskStore.weekCount
      case 'important':
        return taskStore.importantCount
      case 'planned':
        return taskStore.plannedCount
      case 'tasks':
        return taskStore.tasksCount
      case 'completed':
        return taskStore.completedCount
      default:
        return computed(() => taskStore.getTasksByList(listId).length)
    }
  }

  // 带计数的清单列表
  const listsWithCount = computed(() => {
    return lists.value.map(list => ({
      ...list,
      taskCount: getListTaskCount(list.id).value
    }))
  })

  // 自定义清单管理方法
  const addCustomList = (listData: Omit<CustomList, 'id' | 'type' | 'createdAt' | 'updatedAt'>) => {
    const newList: CustomList = {
      id: `custom_${Date.now()}`,
      type: 'custom',
      createdAt: new Date(),
      updatedAt: new Date(),
      ...listData
    }
    customListsData.value.push(newList)
    return newList
  }

  const updateCustomList = (id: string, updates: Partial<Omit<CustomList, 'id' | 'type' | 'createdAt'>>) => {
    const index = customListsData.value.findIndex(list => list.id === id)
    if (index > -1) {
      customListsData.value[index] = {
        ...customListsData.value[index],
        ...updates,
        updatedAt: new Date()
      }
    }
  }

  const deleteCustomList = (id: string) => {
    const index = customListsData.value.findIndex(list => list.id === id)
    if (index > -1) {
      customListsData.value.splice(index, 1)
    }
  }

  // 兼容性方法
  const addList = (name: string, icon?: string, color?: string) => {
    return addCustomList({
      name,
      description: '',
      icon: icon || 'mdi-format-list-bulleted',
      color: color || 'blue',
      isDefault: false
    })
  }

  const deleteList = (id: string) => {
    deleteCustomList(id)
  }

  const updateList = (id: string, updatedList: Partial<List>) => {
    updateCustomList(id, updatedList)
  }

  return {
    // 数据
    lists,
    systemLists,
    customLists,
    listsWithCount,
    
    // 方法
    addCustomList,
    updateCustomList,
    deleteCustomList,
    getListById,
    getListTaskCount,
    
    // 兼容性方法
    addList,
    deleteList,
    updateList
  }
})